package com.portal.back.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.portal.back.service.ISysProfessionalService;
import com.portal.back.service.ISysReSearchSerivce;
import com.portal.back.service.impl.SysProfessionalService;
import com.portal.back.service.impl.SysReSearchService;
import com.portal.base.web.BaseAction;
import com.portal.domain.SysProfessional;
import com.portal.domain.SysReSearch;
import com.portal.util.file.FileUtil;
import com.portal.util.file.ImageSizer;
import com.portal.util.file.XmlUtil;

/**
 * @Title:行业视点
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-13
 * 
 */
@SuppressWarnings("serial")
public class SysReSearchAction extends BaseAction {
    private  ISysReSearchSerivce  service;
	private ISysProfessionalService proservice;
	private  SysReSearch  search;
	private File file;
	private String fileFileName;
	private String fileContentType;
    public SysReSearch getSearch() {
		return search;
	}
	public void setSearch(SysReSearch search) {
		this.search = search;
	}
	public   SysReSearchAction(){
    	service=new SysReSearchService();
    	proservice = new SysProfessionalService();
    }
    public String getAllPro() {
		List<SysProfessional> list = proservice.getAllPro();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getAllpro";
	}
    public  String  addReSearch(){
    	String id = request.getParameter("id");
    	if (file != null) {
			String basePath = request.getSession().getServletContext()
					.getRealPath("/");
			String imagePatn = XmlUtil.getValue("imgPath", "sysConfig") + "/";
			String fileName = new Date().getTime() + fileFileName;
			if (FileUtil.doUpFile(basePath + imagePatn + fileName,file)) {
				search.setAttachUrl(imagePatn+fileName);
			}
			

		}
    	if (id == null || "".equals(id)) {
			boolean  isFlag=service.saveReSearch(search);
			if(isFlag)return "success";
			else  return "error";
			}else{
				SysReSearch  old=service.getById(id);
				if(file==null) search.setAttachUrl(old.getAttachUrl());
				search.setId(Long.parseLong(id));
				boolean  isFlag=service.saveReSearch(search);
				if(isFlag)return "success";
				else  return "error";
			}
    	
    }
     public String  getSearchById(){
    	 List<SysProfessional> list = proservice.getAllPro();
 		ActionContext context = ActionContext.getContext();
 		context.put("list", list);
    		String id = request.getParameter("id");
    		SysReSearch res = service.getById(id);
    	
    		context.put("search", res);
    		return "somesearch";
    	
     }
     public String deleteSearch() {
 		String deleteByIdString = request.getParameter("id");
 		boolean isFlag = service.deleteById(deleteByIdString);
 		response.setContentType("text/html;charset=gbk");
 		PrintWriter out = null;
 		if (isFlag)
 			message = "删除成功";
 		else
 			message = "删除失败";
 		StringBuffer sb = new StringBuffer("[");
 		sb.append("{\"message\":\"" + message + "\"" + "}");
 		sb.append("]");

 		try {
 			out = response.getWriter();
 			out.println(sb.toString());
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 		return null;

 	}

    public  String  getAllSearch(){
    	
    	List<SysReSearch> list = service.getlist();
		ActionContext context = ActionContext.getContext();
		context.put("list", list);
		return "getallresearch";
    	
    }
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
}
